$(function () {
    $("#jqGrid").jqGrid({
        url: '../interview/SettlementList',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', key: true, hidden: true },
            { label: '求职单位名称', name: 'company', index: 'company' , width: 120},
            { label: '求职人员姓名', name: 'empname', index: 'empname' , width: 120},
            { label: '性别', name: 'sex', index: 'sex' , width: 40,formatter:getDictInfo},
            { label: '年龄', name: 'age', index: 'age' , width: 40},
            { label: '岗位名称', name: 'jobname', index: 'jobname', width: 80 },
            { label: '入职日期', name: 'entryDate', index: 'entryDate' , width: 80},
            { label: '工伤参保时间', name: 'injuryInsuredDate', index: 'injury_insured_date' , width: 120},
            { label: '确认状态', name: 'confirmStatus', index: 'confirm_status' , width: 80,formatter:getconfirmStatus,unformat:ungetconfirmStatus},
			{ label: '结算批次', name: 'batchCode', index: 'batch_code', width: 100 },
			{ label: '审核结果', name: 'auditResult', index: 'audit_result' , width: 80,formatter:getauditResult,unformat:ungetauditResult},
			{ label: '审核人', name: 'auditUser', index: 'audit_user' , width: 80},
			{ label: '审核日期', name: 'auditDate', index: 'audit_date' , width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
	    q: {
            empname: null,
            batchcode:null
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		reload: function (event) {
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData : {
                    empname: vm.q.empname,
                    batchcode: vm.q.batchcode
                },
                page:page
            }).trigger("reloadGrid");
		},
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var len=ids.length;
            for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
                if(rowData.confirmStatus!=''&&rowData.confirmStatus!=null&&rowData.confirmStatus!='0')
                {
                    alert("数据已通过，不能删除");
                    return ;
                }
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../interview/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        sysconfirm:function(){
            $.ajax({
                type: "POST",
                url: "../interview/sysconfirm",
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        manconfirm:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            vm.confirm(ids,"1");
        },
        mannoconfirm:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            for(var i=0;i<ids.length;i++){
                var rowData = $("#jqGrid").jqGrid('getRowData',ids[i]);
                if(rowData.batchCode!=""&&rowData.batchCode!=null){
                    alert("姓名："+rowData.empname+"的员工已经做过结算！");
                    return ;
                }
            }
            vm.confirm(ids,"0");
        },
        confirm:function(ids,status){
            var params={"ids":ids.join(","),
                "status":status
            };
            $.ajax({
                type: "POST",
                url: "../interview/confirm",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        settlement:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            for(var i=0;i<ids.length;i++){
                var rowData = $("#jqGrid").jqGrid('getRowData',ids[i]);
                if(rowData.confirmStatus!="1"){
                    alert("姓名："+rowData.empname+"的员工确认不通过！");
                    return ;
                }
                if(rowData.batchCode!=""&&rowData.batchCode!=null){
                    alert("姓名："+rowData.empname+"的员工已经做过结算！");
                    return ;
                }
            }
            utils.openDialogView('选择结算批次',$("#window"),'600px','150px',1);
        },
        addBatchCode:function(){
            var yearmonth = $("#yearmonth").val();
            if(yearmonth==""){
                alert("请选择结算批次!");
                return;
            }
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var params={
                "ids":ids.join(","),
                "yearmonth":yearmonth
            };
            $.ajax({
                type: "POST",
                url: "../interview/addBatchCode",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                            utils.closeCurrentPage();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        close:function(){
            utils.closeCurrentPage();
        },
        audit:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            for(var i=0;i<ids.length;i++){
                var rowData = $("#jqGrid").jqGrid('getRowData',ids[i]);
                if(rowData.confirmStatus!="1"){
                    alert("姓名："+rowData.empname+"的员工确认不通过！");
                    return ;
                }
                if(rowData.batchCode==""||rowData.batchCode==null){
                    alert("姓名："+rowData.empname+"的员工还未结算！");
                    return ;
                }
            }
            $.ajax({
                type: "POST",
                url: "../interview/audit",
                contentType: "application/json",
                data: JSON.stringify({ids:ids.join(",")}),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                            utils.closeCurrentPage();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
	}
});

function getconfirmStatus(cellvalue, options, rowObject){
    var html="";

        if(cellvalue=="0"){
            html="不通过";
        }else if(cellvalue=="1"){
            html="通过";
        }
    return html;
}
function ungetconfirmStatus(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="不通过"){
        html="0";
    }else if(cellvalue=="通过"){
        html="1";
    }
    return html;
}

function getauditResult(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="0"){
        html="未审核";
    }else if(cellvalue=="1"){
        html="已审核";
    }
    return html;
}
function ungetauditResult(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="未审核"){
        html="0";
    }else if(cellvalue=="已审核"){
        html="1";
    }
    return html;
}